/*
 * VCardReaderView.java
 */

package vcardreader;

import java.awt.Component;
import java.io.File;
import org.jdesktop.application.Action;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTextField;
import vcardreader.constants.VCFConstants;
import vcardreader.utilities.ExtensionFileFilter;

/**
 * The application's main frame.
 */
public class VCardReaderView extends FrameView {

    public VCardReaderView(SingleFrameApplication app) {
        super(app);
        initComponents();
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = VCardReaderApp.getApplication().getMainFrame();
            aboutBox = new VCardReaderAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        VCardReaderApp.getApplication().show(aboutBox);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel6 = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        srcCombinedFileField = new javax.swing.JTextField();
        destSplitFilesField = new javax.swing.JTextField();
        srcSplitFilesField = new javax.swing.JTextField();
        destCombinedFileField = new javax.swing.JTextField();
        srcCombinedFileBtn = new javax.swing.JButton();
        destSplitFilesBtn = new javax.swing.JButton();
        splitBtn = new javax.swing.JButton();
        srcSplitFilesBtn = new javax.swing.JButton();
        destCombinedFileBtn = new javax.swing.JButton();
        combineBtn = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        destCombinedFileNameField = new javax.swing.JTextField();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        fileChooser = new javax.swing.JFileChooser();

        mainPanel.setName("mainPanel"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(vcardreader.VCardReaderApp.class).getContext().getResourceMap(VCardReaderView.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setFont(resourceMap.getFont("jLabel5.font")); // NOI18N
        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jSeparator1.setName("jSeparator1"); // NOI18N

        jLabel6.setFont(resourceMap.getFont("jLabel6.font")); // NOI18N
        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jSeparator2.setName("jSeparator2"); // NOI18N

        srcCombinedFileField.setText(resourceMap.getString("srcCombinedFileField.text")); // NOI18N
        srcCombinedFileField.setName("srcCombinedFileField"); // NOI18N

        destSplitFilesField.setText(resourceMap.getString("destSplitFilesField.text")); // NOI18N
        destSplitFilesField.setName("destSplitFilesField"); // NOI18N

        srcSplitFilesField.setText(resourceMap.getString("srcSplitFilesField.text")); // NOI18N
        srcSplitFilesField.setName("srcSplitFilesField"); // NOI18N

        destCombinedFileField.setText(resourceMap.getString("destCombinedFileField.text")); // NOI18N
        destCombinedFileField.setName("destCombinedFileField"); // NOI18N

        srcCombinedFileBtn.setText(resourceMap.getString("srcCombinedFileBtn.text")); // NOI18N
        srcCombinedFileBtn.setName("srcCombinedFileBtn"); // NOI18N
        srcCombinedFileBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                srcCombinedFileBtnActionPerformed(evt);
            }
        });

        destSplitFilesBtn.setText(resourceMap.getString("destSplitFilesBtn.text")); // NOI18N
        destSplitFilesBtn.setName("destSplitFilesBtn"); // NOI18N
        destSplitFilesBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                destSplitFilesBtnActionPerformed(evt);
            }
        });

        splitBtn.setText(resourceMap.getString("splitBtn.text")); // NOI18N
        splitBtn.setName("splitBtn"); // NOI18N

        srcSplitFilesBtn.setText(resourceMap.getString("srcSplitFilesBtn.text")); // NOI18N
        srcSplitFilesBtn.setName("srcSplitFilesBtn"); // NOI18N
        srcSplitFilesBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                srcSplitFilesBtnActionPerformed(evt);
            }
        });

        destCombinedFileBtn.setText(resourceMap.getString("destCombinedFileBtn.text")); // NOI18N
        destCombinedFileBtn.setName("destCombinedFileBtn"); // NOI18N
        destCombinedFileBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                destCombinedFileBtnActionPerformed(evt);
            }
        });

        combineBtn.setText(resourceMap.getString("combineBtn.text")); // NOI18N
        combineBtn.setName("combineBtn"); // NOI18N

        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        destCombinedFileNameField.setName("destCombinedFileNameField"); // NOI18N

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 350, Short.MAX_VALUE)
                            .addComponent(jLabel5)
                            .addGroup(mainPanelLayout.createSequentialGroup()
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2))
                                .addGap(32, 32, 32)
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(mainPanelLayout.createSequentialGroup()
                                        .addComponent(srcCombinedFileField, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(srcCombinedFileBtn, javax.swing.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE))
                                    .addGroup(mainPanelLayout.createSequentialGroup()
                                        .addComponent(destSplitFilesField, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(splitBtn, javax.swing.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE)
                                            .addComponent(destSplitFilesBtn, javax.swing.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE)))))
                            .addComponent(jLabel6)
                            .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 350, Short.MAX_VALUE)
                            .addGroup(mainPanelLayout.createSequentialGroup()
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, mainPanelLayout.createSequentialGroup()
                                        .addComponent(jLabel3)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(destCombinedFileField, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, mainPanelLayout.createSequentialGroup()
                                        .addComponent(jLabel4)
                                        .addGap(54, 54, 54)
                                        .addComponent(srcSplitFilesField, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(srcSplitFilesBtn, javax.swing.GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE)
                                    .addComponent(destCombinedFileBtn, javax.swing.GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE)))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                        .addGap(287, 287, 287)
                        .addComponent(combineBtn, javax.swing.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
                        .addComponent(destCombinedFileNameField, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(9, 9, 9)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(srcCombinedFileField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(srcCombinedFileBtn))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(destSplitFilesField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(destSplitFilesBtn))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(splitBtn)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(12, 12, 12)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(srcSplitFilesField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(srcSplitFilesBtn))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(destCombinedFileField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(destCombinedFileBtn))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(destCombinedFileNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(combineBtn)
                .addContainerGap())
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(vcardreader.VCardReaderApp.class).getContext().getActionMap(VCardReaderView.class, this);
        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        fileChooser.setName("fileChooser");
        fileChooser.setFileFilter(vcfFileFilter);

        setComponent(mainPanel);
        setMenuBar(menuBar);
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Click handler of the Browse button for selecting the combined source vcf file to be split is clicked
     * @param evt 
     */
    private void srcCombinedFileBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_srcCombinedFileBtnActionPerformed
        combinedSrcFile = fileOpenActionPerformed(srcCombinedFileBtn, false, srcCombinedFileField);
    }//GEN-LAST:event_srcCombinedFileBtnActionPerformed

    /**
     * Click handler of the Browse button for selecting the destination folder to place the split files
     * @param evt 
     */
    private void destSplitFilesBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_destSplitFilesBtnActionPerformed
        splitDestDir = fileOpenActionPerformed(destSplitFilesBtn, true, destSplitFilesField);
    }//GEN-LAST:event_destSplitFilesBtnActionPerformed

    /**
     * Click handler of the Browse button for selecting the source folder of the split files to be combined
     * @param evt 
     */
    private void srcSplitFilesBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_srcSplitFilesBtnActionPerformed
       splitSrcDir = fileOpenActionPerformed(srcSplitFilesBtn, true, srcSplitFilesField);
    }//GEN-LAST:event_srcSplitFilesBtnActionPerformed

    /**
     * Click handler of the Browse button for selecting the destination folder where the combined vcf file is to be placed
     * @param evt 
     */    
    private void destCombinedFileBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_destCombinedFileBtnActionPerformed
        combinedDestFile = fileOpenActionPerformed(destCombinedFileBtn, true, destCombinedFileField);
    }//GEN-LAST:event_destCombinedFileBtnActionPerformed
    
    /**
     * Opens the file/directory chooser for the user to select a file/directory
     * @param parent - Button on click of which the file chooser is to be opened
     * @param isDirectory - true if the directory chooser is to be opened. By default the file chooser is opened
     * @param pathField - The field in which the path of the selected file/directory is to be filled
     * @return 
     */
    private File fileOpenActionPerformed(Component parent, boolean isDirectory, JTextField pathField) {
        File selectedFile = null;
        int selectionMode = isDirectory ? JFileChooser.DIRECTORIES_ONLY : JFileChooser.FILES_ONLY;
        fileChooser.setFileSelectionMode(selectionMode);
        //display the file chooser on click of the Open in the File menu
        int status = fileChooser.showOpenDialog(parent);
        if (status == JFileChooser.APPROVE_OPTION) {
            selectedFile = fileChooser.getSelectedFile();
            pathField.setText(selectedFile.getAbsolutePath());
        }
        return selectedFile;
    }    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton combineBtn;
    private javax.swing.JButton destCombinedFileBtn;
    private javax.swing.JTextField destCombinedFileField;
    private javax.swing.JTextField destCombinedFileNameField;
    private javax.swing.JButton destSplitFilesBtn;
    private javax.swing.JTextField destSplitFilesField;
    private javax.swing.JFileChooser fileChooser;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JButton splitBtn;
    private javax.swing.JButton srcCombinedFileBtn;
    private javax.swing.JTextField srcCombinedFileField;
    private javax.swing.JButton srcSplitFilesBtn;
    private javax.swing.JTextField srcSplitFilesField;
    // End of variables declaration//GEN-END:variables
    
    private File combinedSrcFile;
    private File combinedDestFile;
    private File splitDestDir;
    private File splitSrcDir;
    
    private JDialog aboutBox;
    private static ExtensionFileFilter vcfFileFilter = new ExtensionFileFilter(VCFConstants.VCF_EXTENSION, VCFConstants.VCF_FILE_DESCRIPTION);
}
